package com.weinisuo.share.mybatis.plus.table;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class BeautyButlerSourceSelect  {

    public final static List<String> LASTCHAR2 = getLASTCHAR2();

    // private Map<String, DataSource> mapsource = new HashMap<>();

    /*
     * 分库数量
     */
    public static final int DB_COUNT = 4;
    /**
     * 分表个数
     */
    public static final int TABLE_COUNT = 8;

    public List<String> selectDataSource(Object... object) {
        String module = null;
        // HintManager instance = HintManager.getInstance();
        String dataSource = "";
        // String dataSource = DynamicDataSourceHolder.getDataSource();
        List<String> stringList = (List<String>) object[1];

        if (StringUtils.hasText(dataSource)) {
            log.info("=========dataSource:{}==========",dataSource);
            return Arrays.asList(dataSource);
        }
        log.info("==============name:{}===========",object[0]);
        log.info("==============memberId:{}===============",object[1]);

        if (object[1] == null){
            throw new IllegalArgumentException("=========数据源memberId不能为空=======");
        }
        String memberId = String.valueOf(stringList.get(1));
        if ((!StringUtils.hasText(memberId) || "null".equals(memberId)) &&  stringList.size() > 2) {
            String s = stringList.get(2);
            String substring = s.substring(s.length() - 2, s.length());
            Integer id = Integer.valueOf(substring);
            Integer index = id / TABLE_COUNT;
            module =  index < 10 ? "0" + index.toString() : index.toString();
            TablesNameHolder.setThreadTableName(String.valueOf(id));
        }else {
            int mod = getMod(memberId);
            module = getMod4Db(mod);
            TablesNameHolder.setThreadTableName(String.valueOf(mod));
        }
        Integer dataBaseType = (Integer.parseInt(stringList.get(0))) ;
        if (!StringUtils.hasText(module)) {
            throw new IllegalArgumentException("=========数据源memberId取mod4Db异常=======");
        }
        String key = null;
        if (dataBaseType == DataBaseType.BASE_DATA) {
            key = "xws_";
        }else if( dataBaseType == DataBaseType.XDS_BASE_DATA) {
            key = "xds_";
        }
        key = key + module;
        log.info("=========dataSource:{}==========",key);
        return Arrays.asList(key);
    }


    public static List<String> getLASTCHAR2(){
        ArrayList<String> objects = new ArrayList<>(256);
        for (int i = 0; i < 255; i++) {
            String string = new BigInteger(String.valueOf(i), 10).toString(16);
            if (string.length() < 2) {
                string = "0" + string;
            }
            objects.add(string);
        }
        return objects;
    }

    public static String getMod4Db(Integer id) {
        Integer index = id / TABLE_COUNT;
        return index < 10 ? "0" + index.toString() : index.toString();
    }


    public static int getMod(String string) {
        String lastChar = string.substring(string.length() - 2, string.length()).toLowerCase();
        return LASTCHAR2.indexOf(lastChar) % 32;
    }

    public static String getMod8Table(Integer id) {
        Integer index = id % TABLE_COUNT;
        return index < 10 ? "0" + index.toString() : index.toString();
    }




    /**
     * 获取我的购买订单主表的名称
     * @param memberId
     * @return
     */
    public static String getOrderBuyerMainTableName(String memberId) {
        return "order_buyer_main_" + getMod8Table(getMod(memberId));
    }


    public static void main(String[] args) {
        String memberId = "dbd9622409b64ff095c930b6fcf4be30";
        String tableName = getMod8Table(getMod(memberId));
        String mod4Db = getMod4Db(getMod(memberId));
        System.out.println("mod4Db:" + mod4Db);
        System.out.println("tableName:" + tableName);
    }
}
